<?php
namespace app\store\controller\apps\lottery;

use app\store\controller\Controller;
use app\store\model\lottery\Active as ActiveModel;

/**
 * 抽奖活动
 */
class Active extends Controller
{
	/**
	 * 
	 */
	public function index()
	{
		$model = new ActiveModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
	}

	/**
     *  新增记录
     */
    public function add()
    {
        $model = new ActiveModel;
        if (!$this->request->isAjax()) {
            return $this->fetch('add', compact('model'));
        }
        if ($model->add($this->postData('active'))) {
            return $this->renderSuccess('添加成功', url('apps.lottery.active/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新
     */
    public function edit($active_id)
    {
        $model = ActiveModel::get($active_id);
        $goodsList = $model->getGoodsListByActiveId($active_id);
        $prizeList = $model->getPrizeListByActiveId($active_id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model', 'goodsList', 'prizeList'));
        }
        if ($model->edit($this->postData('active'))) {
            return $this->renderSuccess('更新成功', url('apps.lottery.active/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 状态
     */
    public function state($active_id, $state)
    {
        $model = ActiveModel::get($active_id);
        if (!$model->setStatus($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * @param $active_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($active_id)
    {
        $model = ActiveModel::get($active_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}